#! /bin/awk -f
# Author : Raman Rawat
#r="XXX" 
#Replacement="YYY"
# Launch command as 
# awk -v r=XXX -v Replacement=YYYY -f ReplaceTextInFile.awk FileList.txt
# FileList.txt has path of files to be analysed.

BEGIN {
nFileCount=0
}
{ 

#print "length of " $1 "is" length($1) 
if( length($1) > 0) {

	#print "Processing file [",NR,"] ", $1
    # create the sed command for each entry, redirect to cat to remove consecutive blank lines
    #x="sed 's/"r"/"Replacement"/g' "$1" > "$1".tmp"
    #x="sed " r " "$1" | cat -s > "$1".tmp"
    x="cat -s "$1" > "$1".tmp"
    print x
    #system(x)
    
    #astyle --style=ansi --indent-namespaces --indent-switches --suffix=none < $1.tmp
    cmd="astyle --style=ansi --indent-namespaces --indent-switches --suffix=none "$1".tmp"
    #print cmd
    #system(cmd)
    
    # make the new file name
    #z=$0
    #gsub(r, Replacement, z)
    #print "z =" z
    ## move the file back to the original name.
    x="mv "$1".tmp " $1
    #x="cp -f "$1".tmp " $1
    print x
    #system(x)

}
    
}
